﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class SysCodeDetailBLL : IBaseInfoDetail
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly ISysCodeDetailDA sysCodeDetailDA = DataAccess.CreateSysCodeDetailDA();

        public SysCodeDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public SysCodeDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteDetail(List<string> mainID, List<string> subId, List<string> pk3)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                List<SysCodeDetailInfo> sysCodeDetailInfos = new List<SysCodeDetailInfo>();
                SysCodeDetailInfo sysCodeDetailInfo = null;
                for (i = 0; i < mainID.Count; i++)
                {
                    sysCodeDetailInfo = sysCodeDetailDA.GetSysCodeDetailByID(dataBase, tran, mainID[i], subId[i], this.dataBaseID[0]);
                    sysCodeDetailInfos.Add(sysCodeDetailInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (sysCodeDetailDA.DeleteSysCodeDetail(dataBase, tran, mainID, subId, this.dataBaseID) > 0)
                {
                    for (i = 0; subId.Count > i; i++)
                    {
                        auditDataBLL.InsertAuditData<SysCodeDetailInfo>(dataBase, tran, sysCodeDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "SYS_CODE_DETAIL", this.dataBaseID);
                        temp = temp + subId[i] + ",";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllDetailInfo()
        {
            throw new NotImplementedException();
        }

        public DataSet GetDetaiInfolByQueryEntity(object obj)
        {
            SysCodeDetailQueryEntity sysCodeDetailQuery = obj as SysCodeDetailQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(sysCodeDetailQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + sysCodeDetailQuery.IsActive);
            }
            return sysCodeDetailDA.GetSysCodeDetailByQueryList(sqlWhere, sysCodeDetailQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public object GetDetailInfoByID(string sysCodeType, string sysCode, string pk3)
        {
            return sysCodeDetailDA.GetSysCodeDetailByID(sysCodeType, sysCode, this.dataBaseID[0]);
        }

        public List<object> GetDetailInfoListByID(string mainID, string subID)
        {
            throw new NotImplementedException();
        }

        public string InsertDetail(object obj)
        {
            string SysCode = string.Empty;
            try
            {
                SysCodeDetailInfo sysCodeDetai = obj as SysCodeDetailInfo;
                if (sysCodeDetailDA.CheckSysCodeDetailIDUnique(sysCodeDetai.SysCode, sysCodeDetai.SysCodeType, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F1800_001", this.pubParasInfo.UserLanguage, new List<string> { sysCodeDetai.SysCode });
                }
                sysCodeDetai.CreatedDate = DateTime.Now;
                sysCodeDetai.UpdatedDate = DateTime.Now;
                sysCodeDetai.UpdatedBy = sysCodeDetai.CreatedBy;
                if (sysCodeDetailDA.InsertSysCodeDetail(sysCodeDetai, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<SysCodeDetailInfo>(null, sysCodeDetai, AuditAction.Insert, this.pubParasInfo.UserID, "SYS_CODE_DETAIL", this.dataBaseID[0]);
                    SysCode = sysCodeDetai.SysCode;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return SysCode;
        }

        public string UpdateDetail(object obj)
        {
            string sysCodeType = string.Empty;
            try
            {
                SysCodeDetailInfo syscodeDetailInfo = obj as SysCodeDetailInfo;
                SysCodeDetailInfo oldSysCodeDetailInfo = sysCodeDetailDA.GetSysCodeDetailByID(syscodeDetailInfo.SysCodeType, syscodeDetailInfo.SysCode, this.dataBaseID[0]);
                if (sysCodeDetailDA.UpdateSysCodeDetail(syscodeDetailInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<SysCodeDetailInfo>(oldSysCodeDetailInfo, syscodeDetailInfo, AuditAction.Update, this.pubParasInfo.UserID, "SYS_CODE_DETAIL", this.dataBaseID[0]);
                    sysCodeType = syscodeDetailInfo.SysCodeType;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return sysCodeType;
        }

        public string UpdateIsActive(List<string> mainID, List<string> subId, string isActive)
        {
            throw new NotImplementedException();
        }
    }
}

